package com.example.jpa1.dao.impl;

import com.example.jpa1.dao.DeskDao;
import com.example.jpa1.pojo.Desk;
import com.example.jpa1.pojo.Student;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

@Repository
public class DeskDaoImpl implements DeskDao {


    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public Desk save(Desk desk) {
        entityManager.persist(desk);
        return null;
    }

    @Override
    public List<Desk> list() {
       String hql = "from desk ";
        TypedQuery<Desk> query = entityManager.createQuery(hql, Desk.class);
        return query.getResultList();
    }

    @Override
    public Desk edit(Desk desk) {
//        entityManager.
        return entityManager.merge(desk);

    }

    @Override
    public void deleteById(Long id) {
//        Desk desk = new Desk().setId(id);报错，游离态
        Desk desk =  entityManager.find(Desk.class,id);//先获取托管态
        entityManager.remove(desk);
    }

}
